﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Main_HouseUpdate : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    HouseDao hd = new HouseDao();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (!IsPostBack)
            {
                UsersVo uv = new UsersVo();
                uv = (UsersVo)Session["user"];
                if (uv == null)
                {
                    Response.Redirect("../index.aspx");
                }
                if (uv.Ugroup == "管理员")
                {
                    string hid = em.Decript(Request.QueryString["hid"]);
                    if (hid == null | hid == "")
                    {
                        Response.Redirect("HouseData.aspx");
                    }
                    else
                    {
                        HouseVo hv = new HouseVo();
                        hv = hd.getSingleHouse(hid);
                        txtname.Text = hv.Hname;
                        if (hv.Hcost == 0)
                        {
                            txtcost.Text = "待定";
                        }
                        else
                        {
                            txtcost.Text = hv.Hcost.ToString();
                        }
                        txtintrduce.Text = hv.Hintroduce.Replace("&nbsp;&nbsp;&nbsp;", " ").Replace("<br>", "\r\n");
                        txtcontent.Text = hv.Hcontent.Replace("&nbsp;&nbsp;&nbsp;", " ").Replace("<br>", "\r\n");
                        txtarea.Text = hv.Harea;
                        txtaddress.Text = hv.Haddress;
                        txttime.Text = hv.Htime;
                        txttel.Text = hv.Htel;
                        txtperson.Text = hv.Hperson;
                        txtcenter.Text = hv.Hcenter;
                        txtdeveloper.Text = hv.Hdeveloper;
                        txtcompany.Text = hv.Hcompany;
                        if (hv.Hlogo != "")
                        {
                            imglogo.ImageUrl = hv.Hlogo;
                        }
                        else
                        {
                            imglogo.Visible = false;
                        }
                        if (hv.Hpic != "")
                        {
                            imgtop.ImageUrl = hv.Hpic;
                        }
                        else
                        {
                            imgtop.Visible = false;
                        }
                        ddltype.SelectedValue = hv.Htype;
                    }
                }
                else
                {
                    Response.Redirect("../index.aspx");
                }
            }
        }
    }
    protected void btnuplogo_Click(object sender, EventArgs e)
    {
        bool fileOK = false;
        string path = Server.MapPath("../images/house/logo/") + fulogo.FileName;
        if (fulogo.FileName != "")
        {
            if (fulogo.HasFile)
            {
                string fileExtension = Path.GetExtension(fulogo.FileName).ToLower();
                string[] allowerExtensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
                for (int i = 0; i < allowerExtensions.Length; i++)
                {
                    if (fileExtension == allowerExtensions[i])
                    {
                        fileOK = true;
                    }
                }
                if (!fileOK)
                {
                    string script = "<script>alert('图片格式不正确！');</script>";
                    Page.RegisterClientScriptBlock("", script);
                    return;
                }
                else
                {
                    string picPath = path.Replace(fulogo.FileName, txtname.Text + "Logo" + fileExtension);
                    fulogo.PostedFile.SaveAs(picPath);
                    string script = "<script>alert('上传图片成功！');</script>";
                    Page.RegisterClientScriptBlock("", script);
                    imglogo.ImageUrl = "../images/house/logo/" + txtname.Text + "Logo" + fileExtension;
                    imglogo.Visible = true;
                }
            }
            else
            {
                string script = "<script>alert('上传失败，格式不允许！');</script>";
                Page.RegisterClientScriptBlock("", script);
            }
        }
        else
        {
            string script = "<script>alert('上传不能为空！');</script>";
            Page.RegisterClientScriptBlock("", script);
        }
    }

    protected void btnupdate_Click(object sender, EventArgs e)
    {
        if (ddltype.SelectedValue == "0")
        {
            string script = "<script>window.alert('请选择所属类型！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        string hid = em.Decript(Request.QueryString["hid"]);
        HouseVo hv = new HouseVo();
        hv.Hid = Convert.ToInt32(hid);
        hv.Hname = txtname.Text;
        hv.Hcost = Convert.ToInt32(txtcost.Text);
        hv.Hintroduce = txtintrduce.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
        hv.Hcontent = txtcontent.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
        hv.Harea = txtarea.Text;
        hv.Haddress = txtaddress.Text;
        hv.Htime = txttime.Text;
        hv.Htel = txttel.Text;
        hv.Hperson = txtperson.Text;
        hv.Hcenter = txtcenter.Text;
        hv.Hdeveloper = txtdeveloper.Text;
        hv.Hcompany = txtcompany.Text;
        hv.Hlogo = imglogo.ImageUrl;
        hv.Hpic = imgtop.ImageUrl;
        hv.Hmap = "";
        hv.Htype = ddltype.SelectedValue;
        hv.Hclass = "";
        Exception ex = hd.updateSingleHouse(hv);
        if (ex == null)
        {
            string script = "<script>window.alert('修改成功！');location.href='HouseData.aspx';</script>";
            Page.RegisterClientScriptBlock("", script);
        }
        else
        {
            string script = "<script>window.alert('修改失败！');</script>";
            Page.RegisterClientScriptBlock("", script);
        }
    }
    protected void btnuptop_Click(object sender, EventArgs e)
    {
        bool fileOK = false;
        string path = Server.MapPath("../images/house/pic/") + futop.FileName;
        if (futop.FileName != "")
        {
            if (futop.HasFile)
            {
                string fileExtension = Path.GetExtension(futop.FileName).ToLower();
                string[] allowerExtensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
                for (int i = 0; i < allowerExtensions.Length; i++)
                {
                    if (fileExtension == allowerExtensions[i])
                    {
                        fileOK = true;
                    }
                }
                if (!fileOK)
                {
                    string script = "<script>alert('图片格式不正确！');</script>";
                    Page.RegisterClientScriptBlock("", script);
                    return;
                }
                else
                {
                    string picPath = path.Replace(futop.FileName, txtname.Text + "top" + fileExtension);
                    futop.PostedFile.SaveAs(picPath);
                    string script = "<script>alert('上传图片成功！');</script>";
                    Page.RegisterClientScriptBlock("", script);
                    imgtop.ImageUrl = "../images/house/pic/" + txtname.Text + "top" + fileExtension;
                    imgtop.Visible = true;
                }
            }
            else
            {
                string script = "<script>alert('上传失败，格式不允许！');</script>";
                Page.RegisterClientScriptBlock("", script);
            }
        }
        else
        {
            string script = "<script>alert('上传不能为空！');</script>";
            Page.RegisterClientScriptBlock("", script);
        }
    }
}
